package main

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func main() {
	go func() {
		time.Sleep(time.Second * 5)
		request()
	}()
	http.HandleFunc("/1", testHttp)
	err := http.ListenAndServe(":8090", nil)
	if err != nil {
		fmt.Println("http err :", err)
	}
}

type server interface {
	ListenAndServe() error
}

func getServer() server {
	return &http.Server{}
}

type e1 interface {
	Error() string
}

func test1() e1 {
	x := errors.New("123")
	return x
}

func testHttp(w http.ResponseWriter, req *http.Request) {
	fmt.Println("res :", "Hi ")

	fmt.Println("===================")
	fmt.Println("res :", req.Method)
	fmt.Println("===================")
	body, _ := ioutil.ReadAll(req.Body)
	fmt.Println("res :", string(body))
	fmt.Println("===================")

	w.Write([]byte("e . e."))

}

func request() {
	str := `{"Test":1,"name":"li"}`
	req, err := http.NewRequest("GET", "http://127.0.0.1:8090/1", bytes.NewBuffer([]byte(str)))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("req :", req.Body)

	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println("req :", err)
		return
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	fmt.Println("req :", string(body))
}

func testMiddle() {
	mux := http.NewServeMux()
	http.ListenAndServe("", mux)
	http.Serve(nil, nil)
	_ = mux
}
